package com.app.micaihu.view.army.fragment

import androidx.lifecycle.Observer
import com.app.base.BaseMvpFragment
import com.app.http.bean.BaseBean
import com.app.micaihu.R
import com.app.micaihu.configure.Config
import com.app.micaihu.view.army.contract.ArmyListContract
import com.app.micaihu.view.army.presenter.ArmyListPresenter
import com.app.micaihu.view.bean.ArmyList
import com.app.utils.biz.Biz
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ViewUtils
import com.jeremyliao.liveeventbus.LiveEventBus

/**
 * 军团列表
 */
class ArmyListFragment : BaseMvpFragment<ArmyListContract.View, ArmyListPresenter>(), ArmyListContract.View {

    override fun getLayoutId() = R.layout.army_list_fragment
    override fun getPresenter() = ArmyListPresenter()

    override fun setData() {
        mPresenter?.refreshArmyList()
    }

    private fun initFragment(isJoinArmy: Boolean) {
        val transaction = childFragmentManager.beginTransaction()
        if (Biz.instance.uid().isEmpty() || !isJoinArmy) {
            transaction.replace(R.id.flContent, NotJoinedArmyListFragment())
        } else {
            transaction.replace(R.id.flContent, JoinedArmyListFragment())
        }
        transaction.commitAllowingStateLoss()
    }

    override fun lazyLoad() {

    }

    override fun addListeners() {
        LiveEventBus.get(Config.EventBus.loginSuccess, Boolean::class.java).observe(this, Observer {
            LogUtils.e(classTag, "loginSuccess->" + it)
            if (it) {
                ViewUtils.runOnUiThreadDelayed({ mPresenter?.refreshArmyList() }, 100)
            }
        })
        LiveEventBus.get(Config.EventBus.logOutSuccess, Boolean::class.java).observe(this, Observer {
            LogUtils.e(classTag, "logOutSuccess->" + it)
            if (it) {
                ViewUtils.runOnUiThreadDelayed({ mPresenter?.refreshArmyList() }, 100)
            }
        })
        LiveEventBus.get(Config.EventBus.joinArmySuccess, Boolean::class.java).observe(this, Observer {
            LogUtils.e(classTag, "joinArmySuccess->" + it)
            if (it) {
                ViewUtils.runOnUiThreadDelayed({ mPresenter?.refreshArmyList() }, 100)
            }
        })

        LiveEventBus.get(Config.EventBus.signOutArmySuccess, Boolean::class.java).observe(this, Observer {
            LogUtils.e(classTag, "signOutArmySuccess->" + it)
            if (it) {
                ViewUtils.runOnUiThreadDelayed({ mPresenter?.refreshArmyList() }, 100)
            }
        })

        LiveEventBus.get(Config.EventBus.armyCreateSuccess, Boolean::class.java).observe(this, Observer {
            LogUtils.e(classTag, "armyCreateSuccess->" + it)
            if (it) {
                ViewUtils.runOnUiThreadDelayed({ mPresenter?.refreshArmyList() }, 100)
            }
        })
    }

    override fun handleArmyList(result: BaseBean<ArmyList?>?, loadType: Int) {
        if (result == null) {
            initFragment(false)
        } else {
            if (result.isSuccess()) {
                val data = result.data
                if (data == null) {
                    initFragment(false)
                } else {
                    val myArmyGroup = data.myArmyGroup
                    if (myArmyGroup == null) {
                        initFragment(false)
                    } else {
                        // 0待审核 1已通过 2拒绝 3退出或删除
                        if (myArmyGroup.status == "1") {
                            initFragment(true)
                        } else {
                            initFragment(false)
                        }
                    }
                }
            } else {
                initFragment(false)
            }
        }
    }
}